/**
 * @title HHUsertypeAction.java 
 * @description 	用户类型控制层类
 * @create 			2012-8-14 上午10:38:31 By xjiujiu
 * @package 		apps.admin.action
 * @copyright 		Copyright (c) 2011-2012 http://xyrj.hhtc.edu.cn.com. All right reserved
 * @version         $Id$
 * XYRJ-Java-Project
 */

package apps.admin.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import system.config.HHConfig;
import system.pojo.HHUserTypePojo;

import com.xyrj.exception.HHUploadFileException;
import com.xyrj.exception.HHValidateException;
import com.xyrj.filesystem.HHFile;
import com.xyrj.utils.HHDatetime;


import apps.admin.form.HHAddUsertypeForm;
import apps.admin.model.HHModelBaseModel;
import apps.admin.model.HHUserTypeModel;

/** 
 * 用户类型动作层类
 * 
 * @description
 * 
 * @classname 	HHUsertypeAction 
 * @author 		xjiujiu<xjiujiu@foxmail.com>
 * @date 		2012-8-14 上午10:38:31 
 * @version 	1.0
 */
public class HHUsertypeAction extends HHActionBaseAction
{

	/** 
     * 构造函数
     *
     * @see HHActionBaseAction#HHActionBaseAction(HttpServletRequest, HttpServletResponse)
     */
    public HHUsertypeAction(HttpServletRequest request,
            HttpServletResponse response)
    {
	    super(request, response);
	    
	    this._pojo 	= new HHUserTypePojo();
    }

    /**
     * @see HHActionBaseAction#addview()
     */
    public void addview()
    {
    	super.addview();
    	this._assignParentList(new HHUserTypeModel());
    	
    	this._view.display("admin/usertype/info.jsp");
    }
    
    /**
     * 添加记录动作
     *  
     * @description
     * 
     * @title  add
     * @return void
     */
    public void add()
    {
    	try {
	    	this._assginPojoByAdd();
	    	HHAddUsertypeForm 	addUserTypeForm = new HHAddUsertypeForm();
	    	addUserTypeForm.validate(this._pojo);
	    	this._uploadFile("image_path");
	    	this._pojo.setField("edit_time", HHDatetime.getNow());
	    	this._pojo.setField("create_time", HHDatetime.getNow());
	    	this._pojo.setField("author", this._view.getSessionAttr("xyrj_user_name"));
	    	HHUserTypeModel userTypeModel 		= this._initModel();
	    	if(0 <  userTypeModel.add(this._pojo)) {
	    		this._view.alertAndGoBack("添加成功");
	    	} else {
	    		HHFile.deleteFile(
    				HHConfig.ROOT_PATH + "/" +
		    		(String)this._pojo.getField("image_path")
	    		);
	    		this._view.alertAndGoBack("添加失败！请联系管理员～");
	    	}
    	} catch (HHValidateException ex) {
    		this._view.alertAndGoBack(ex.getMessage());
    	} catch (HHUploadFileException ex) {
    		this._view.alertAndGoBack(ex.getMessage());
    	}
    }

    /**
     * @see HHActionBaseAction#editview()
     */
    public void editview()
    {
    	super.editview();
    	this._assignParentList(new HHUserTypeModel());
    	
    	this._view.display("admin/usertype/info.jsp");
    }
    
    /**
     * 修改记录动作
     *  
     * @description
     * 
     * @title  edit
     * @return void
     */
    public void edit()
    {
    	try {
	    	this._assginPojoByEdit();
	    	HHAddUsertypeForm 	editUserTypeForm = new HHAddUsertypeForm();
	    	editUserTypeForm.validate(this._pojo);
	    	this._uploadFile("image_path");
	    	this._pojo.setField("edit_time", HHDatetime.getNow());
	    	this._pojo.setField("author", this._view.getSessionAttr("xyrj_user_name"));
	    	HHUserTypeModel userTypeModel 		= this._initModel();
	    	if(0 <  userTypeModel.edit(this._pojo)) {
	    		this._deleteOldFile("image_path");
	    		this._view.alertAndRefresh("更新成功");
	    	} else {
	    		HHFile.deleteFile(
    				HHConfig.ROOT_PATH + "/" +
		    		(String)this._pojo.getField("image_path")
	    		);
	    		this._view.alertAndRefresh("更新失败！请联系管理员～");
	    	}
    	} catch (HHValidateException ex) {
    		this._view.alertAndGoBack(ex.getMessage());
    	} catch (HHUploadFileException ex) {
    		this._view.alertAndGoBack(ex.getMessage());
    	}	
    }
   
	/* (非 Javadoc) 
	 * <p>得到当前模块数据层操作对象</p>
	 * 
	 * <p>description: </p> 
	 *
	 * @title _initModel 
	 * @see apps.admin.action.HHActionBaseAction#_initModel() 
	 */
	@Override
	protected HHUserTypeModel _initModel()
	{
		return new HHUserTypeModel(this._pojo);
	}

	/* (非 Javadoc) 
     * <p>得到当前模块的所属父类实例</p>
     * 
     * <p>description: </p> 
     *
     * @title _initParentModel 
     * @return 
     * @see apps.admin.action.HHActionBaseAction#_initParentModel() 
     */
    @Override
    protected HHModelBaseModel _initParentModel()
    {
	    return this._initModel();
    }

}
